﻿using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace Mp3Observer.Common
{
    // http://blog.ploeh.dk/2010/02/03/ServiceLocatorIsAnAntiPattern.aspx
    public interface IServiceLocator { T Resolve<T>();}
    public class Locator : IServiceLocator 
    { 
        private readonly Dictionary<Type, Func<object>> services; 
        public Locator()
        { 
            this.services = new Dictionary<Type, Func<object>>();
        } 
        public void Register<T>(Func<T> resolver) 
        {
            this.services[typeof(T)] = () => resolver();
        }
        public T Resolve<T>()
        {
            return (T)this.services[typeof(T)](); 
        }
    }

    /// <summary>
    /// Implements a simple service locator
    /// </summary>
    public class ServiceLocator : IServiceProvider
    {
        /// <summary>
        /// the dictionary of services.
        /// </summary>
        private Dictionary<Type, object> services = new Dictionary<Type, object>();

        #region IServiceProvider Members

        /// <summary>
        /// Gets a service from the service locator
        /// </summary>
        /// <typeparam name="T">The type of service you want to get</typeparam>
        /// <returns>Returns the instance of the service</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "ok so")]
        [DebuggerStepThrough]
        public T GetService<T>()
        {
            return (T)this.GetService(typeof(T));
        }

        /// <summary>
        /// Registers a service to the service locator
        /// </summary>
        /// <typeparam name="T">type of the service</typeparam>
        /// <param name="service">The service to add</param>
        /// <param name="overwriteIfExists">Passing true will replace any existing service</param>
        /// <returns>
        /// Returns true if the service was successfully registered
        /// </returns>
        /// <remarks>
        /// 	<para>This generics based implementation ensures that the service must at least inherit from the service type.</para>
        /// 	<para>NOTE: the MSDN documentation on IServiceProvidor states that the GetService method returns an object of type servieProvider</para>
        /// </remarks>
        public bool RegisterService<T>(T service, bool overwriteIfExists = false)
        {
            lock (this.services)
            {
                if (!this.services.ContainsKey(typeof(T)))
                {
                    this.services.Add(typeof(T), service);
                    return true;
                }
                else if (overwriteIfExists)
                {
                    this.services[typeof(T)] = service;
                    return true;
                }
            }
            return false;
        }

        ///// <summary>
        ///// Registers a service to the service locator. This will overwrite any registered services with the same registration type
        ///// </summary>
        ///// <typeparam name="T">type of the service</typeparam>
        ///// <param name="service">The service to add</param>
        ///// <returns>
        ///// Returns true if the service was successfully registered
        ///// </returns>
        ///// <remarks>
        ///// 	<para>This generics based implementation ensures that the service must at least inherit from the service type.</para>
        ///// 	<para>NOTE: the MSDN documentation on IServiceProvidor states that the GetService method returns an object of type servieProvider</para>
        ///// </remarks>
        //public bool RegisterService<T>(T service)
        //{
        //    return RegisterService<T>(service, true);
        //}

        /// <summary>
        /// Gets a service from the service locator
        /// </summary>
        /// <param name="serviceType">The type of service you want to get</param>
        /// <returns>Returns the instance of the service</returns>
        /// <remarks>This implements IServiceProvider</remarks>
        [DebuggerStepThrough]
        public object GetService(Type serviceType)
        {
            lock (this.services)
            {
                if (this.services.ContainsKey(serviceType))
                    return this.services[serviceType];
            }
            return null;
        }

        #endregion
    }
}
